/*
  学习目标：组件通信-兄弟组件-状态提升
  状态提升:
    将兄弟组件需要共享的数据, 提升到共同的父组件中. 然后可以父传子/子传父实现共享数据

*/

import React, { Component } from 'react';
import Husband from './components/Husband';
import Wife from './components/Wife';

export default class App extends Component {
  // 1. 状态提升到父组件中
  state = {
    money: 0,
  };

  /* 4. 赚钱方法 */
  handleMakeMoney = () => {
    this.setState({ money: this.state.money + 1000 });
  };

  // 5. 定义函数的回调函数
  handleCost = (num) => {
    this.setState({ money: this.state.money - num });
  };

  render() {
    const { money } = this.state;
    return (
      <div>
        <h1 style={{ textAlign: 'center' }}>家庭存款：</h1>
        <Husband
          // 2. 父传子state
          // 4,
          handleMakeMoney={this.handleMakeMoney}
          money={money}
        ></Husband>
        <hr />
        <Wife
          // 6. 给子组件传递回调函数
          handleCost={this.handleCost}
        ></Wife>
      </div>
    );
  }
}
